﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BranchOfSansar.Sansar.Register;

namespace BranchOfSansar.Register.CallCenter
{
    public partial class frmClose : Form
    {
        private long RequestNo = 0;
        public frmClose(long req_no)
        {
            InitializeComponent();
            RequestNo = req_no;
        }

        private void frmClose_Load(object sender, EventArgs e)
        {
            txtUser.Text = Globals.UserName;
            txtDate.Text = Globals.SystemDate.ToString("yyyy.MM.dd");
            using (BranchOfSansar.Sansar.Enquiry.EnquiryClient ec = new Sansar.Enquiry.EnquiryClient(Globals.Token))
            {
                txtReason.Properties.Items.AddRange(ec.Parameters("it.GroupCode='CC' ").Select(s => new BranchOfSansar.Sansar.Enquiry.ComboItem() { StrValue = s.ParaCode, Display = s.Name }).ToArray());
                txtReason.SelectedIndex = 0;
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtReason.Text))
            {
                Globals.Warr("Тайлбар хоосон байна!");
                return;
            }
            using (RegisterClient rc = new RegisterClient(Globals.Token))
            {
                CallComment save = new CallComment()
                {
                    CommentDate = Globals.SystemDate,
                    Comment = txtComment.Text,
                    UserNo = Globals.UserId,
                    ReqNo = RequestNo
                };
                Result res = rc.CloseCallRequest(save, RequestNo, ((BranchOfSansar.Sansar.Enquiry.ComboItem)txtReason.SelectedItem).StrValue);
                if (res.Code != 0)
                {
                    Globals.Warr(res.Message);
                }
                else
                {
                    Globals.Info(res.Message);
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }
    }
}
